// pages/PayOrderFinishPage/PayOrderFinishPage.js
var screenUtil = require("../../utils/screen.js");
var api = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tuangouID:'',
    code:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.data.tuangouID = options.tuangouId;
    that.data.code = options.code;
    var screen = screenUtil.screenUtil()
    that.setData({
      isIphoneX: getApp().globalData.statusBarHeight>=44?true:false,
      btmHeight:120,
      scrWidth: screen.windowWidth,
      code:that.data.code
    })
    
  },
  gotoOrder:function(){
    var that = this
    wx.requestSubscribeMessage({
      tmplIds: ['zuR6VGvawACL2TiDPzs8UtXIgSLc108mZujAseIh25g','sF7fXkqI4dSVH2HNFEXWdc2u92xiWSVWO73D_bpmJfE','3EaSIVA3ijVxVtoCqU9Rp2DxpNVjl2iVjAghWpQpThw'],	//需要订阅的消息模板的id的集合，一次调用最多可订阅3条消息
      success (res) {
        console.log("3333");
        var result = res["zuR6VGvawACL2TiDPzs8UtXIgSLc108mZujAseIh25g"];
        that.judgeUserRemind("zuR6VGvawACL2TiDPzs8UtXIgSLc108mZujAseIh25g","2",(result=='accept'?'1':'0'))
        var result2 = res["sF7fXkqI4dSVH2HNFEXWdc2u92xiWSVWO73D_bpmJfE"];
        that.judgeUserRemind("sF7fXkqI4dSVH2HNFEXWdc2u92xiWSVWO73D_bpmJfE","1",(result2=='accept'?'1':'0'))
        var result3 = res["3EaSIVA3ijVxVtoCqU9Rp2DxpNVjl2iVjAghWpQpThw"];
        that.judgeUserRemind("3EaSIVA3ijVxVtoCqU9Rp2DxpNVjl2iVjAghWpQpThw","4",(result3=='accept'?'1':'0'))

        }  
    })

  },
  judgeUserRemind:function(num,typeNum,yesNo){
    var that = this
    let id = num
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        //console.log(res)
        if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
          if (res.subscriptionsSetting.itemSettings) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
            let moIdState = res.subscriptionsSetting.itemSettings[id]; // 用户同意的消息模板id
            if (moIdState === 'accept') {
              getApp().globalData.isCheck1 = 1
              //console.log('接受了消息推送');
              api.request1({ "path": "user/remind","tuangouId":that.data.tuangouID,"state":"1","type":typeNum,"token":wx.getStorageSync('token')}, function (result) {     
                if(result.code=='0'){
                  wx.reLaunch({
                    url: '/pages/CurrentOrderList/CurrentOrderList?tuangouId='+that.data.tuangouID,
                  })
                }
              }, function (error) {
              })
  
            } else if (moIdState === 'reject') {
              //console.log('拒绝了消息推送');
              wx.reLaunch({
                url: '/pages/CurrentOrderList/CurrentOrderList?tuangouId='+that.data.tuangouID,
              })
              
            } else if (moIdState === 'ban') {
              //console.log("已被后台封禁");
              wx.reLaunch({
                url: '/pages/CurrentOrderList/CurrentOrderList?tuangouId='+that.data.tuangouID,
              })
            }else{
              api.request1({ "path": "user/remind","tuangouId":that.data.tuangouID,"state":"1","type":typeNum,"token":wx.getStorageSync('token')}, function (result) {     
                if(result.code=='0'){
                  wx.reLaunch({
                    url: '/pages/CurrentOrderList/CurrentOrderList?tuangouId='+that.data.tuangouID,
                  })
                }
              }, function (error) {
              })
            }
          } else {
            api.request1({ "path": "user/remind","tuangouId":that.data.tuangouID,"state":"0","type":typeNum,"token":wx.getStorageSync('token')}, function (result) {     
              if(result.code=='0'){
                wx.reLaunch({
                  url: '/pages/CurrentOrderList/CurrentOrderList?tuangouId='+that.data.tuangouID,
                })
              }
            }, function (error) {
            })
          }
        } else {
          wx.reLaunch({
            url: '/pages/CurrentOrderList/CurrentOrderList?tuangouId='+that.data.tuangouID,
          })
          wx.openSetting({
            withSubscriptions: true,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})